
/*
HappyFlags_4.pde --Timer commands usually take a channel number and/or a time-value.

Copyright (c) 2010 Ed Bennett.  All rights reserved. 

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

//
// The unit of time is is called a blink. The duration 
// of a blink is related to an AVR jiffy. See
// http://en.wikipedia.org/wiki/Jiffy_%28time%29

#include <TimerScheduler.h>

// hardware pins
int HF0 = 13;
int HF1 = 12;
int HF2 = 11;
int HF3 = 10;
int HF4 = 9;
int HF5 = 8;
int HF6 = 7;
int HF7 = 6;

int rate = 0;

int outBitVal7 = 0;

int outval[8]={
  0,0,0,0,0,0,0,0};
  
int i;
int whichOne =0;

void setup()
{   

  pinMode(HF0, OUTPUT); 
  pinMode(HF1, OUTPUT); 
  pinMode(HF2, OUTPUT);
  pinMode(HF3, OUTPUT); 
  pinMode(HF4, OUTPUT);
  pinMode(HF5, OUTPUT); 
  pinMode(HF6, OUTPUT);
  pinMode(HF7, OUTPUT); 

  Timer.begin();        // start the hardware timer

  Timer.attachPin(0, HF0);    // (channel, Ard. pin number)
  Timer.attachPin(1, HF1); 
  Timer.attachPin(2, HF2); 
  Timer.attachPin(3, HF3); 
  Timer.attachPin(4, HF4);
}

void loop()
{

  if(i>=5){
    i=0;
  }

  if(outval[i]<5){
    outval[i]=1000;
    outBitVal7 ^= 1;
    digitalWrite(HF7, outBitVal7); // rollover indicator lamp
      if(i%2 == 0){
         whichOne = constrain(whichOne, 0, 4);
      }
  }

  Timer.mono(i, outval[i]); 

  if(rate >300)rate=7;
  rate = rate * 10/9;

  Timer.mono(7, rate);  // simple delay
  while(!(Timer.flagIsSet(7)));

  outval[i]= outval[i] * 90/100;
  
  i++; 

}









